<?php

class MenuController extends Controller {

    public $model = "";

    public function actionIndex() {
        $model = new Menu();
        $this->performAjaxValidation($model);
        if ( Yii::app()->request->getIsPostRequest()) {
            $model->attributes = Yii::app()->request->getPost('Menu');
            if ($model->save()) {
                Yii::app()->cookies->menu_parent_id =  (int)$model->menu_parent_id;
                $this->redirect($this->createUrl('index'));
            }
        }
        $this->model = $model;
        $this->model->menu_parent_id = (int) Yii::app()->cookies->menu_parent_id;
        unset($model);
        $this->render('index', array('model' => $this->model));
    }

    public function actionSave() {
        $menu = new Menu();
        $model = $menu->findByPk(Yii::app()->request->getPost("menu_id"));
        if (isset($_POST['menu_id'])) {
            $model->attributes = $_POST;
            if ($model->save()) {
                Yii::app()->cookies->menu_parent_id =  (int)$model->menu_parent_id;
                RMessage::set('true', 'success');
            }else{
                RMessage::set('false', $model->getErrors());
            }
        }else{
            RMessage::set('false', 'data is empty');
        }
        unset($model);
        exit;
    }

    public function actionAdd() {
        $model = new Menu();
        $this->performAjaxValidation($model);
        $request = Yii::app()->request;
        if ( $request->getIsPostRequest() ) {
            $model->attributes = $request->getPost('Menu');
            if ($model->save()) {
                Yii::app()->cookies->menu_parent_id =  (int)$model->menu_parent_id;
                echo CHtml::tag('div', array('class' => 'ok'), Yii::t('default', '保存成功!'));
            } else {
                echo CHtml::errorSummary($model);
            }
        }
    }

    public function actionEdit() {
        $model = new Menu();
        $id = intval(Yii::app()->request->getParam("id"));
        $menu = $model->findByPk($id);
        $request = Yii::app()->request;
        if ( $request->getIsPostRequest()  ) {
            $this->performAjaxValidation($model);
            $menu->attributes = $request->getPost('Menu');
            if ($menu->save()) {
                Yii::app()->cookies->menu_parent_id =  (int)$model->menu_parent_id;
                $this->redirect($this->createUrl('index'));
            }
        }
        $this->render('edit', array('model' => $model, 'modify' => $menu));
    }

    public function actionJson() {
        $model = new Menu();
        $data = $model->findAll("menu_parent_id=0");
        $result = array();
        $res = array();
        $result['total'] = $model->count();
        foreach ($data as $var) {
            $res['menu_id'] = $var['menu_id'];
            $res['menu_name'] = $var['menu_name'];
            $res['children'] = $model->findAll("menu_parent_id=:id", array(':id' => $var['menu_id']));
            $result['rows'][] = $res;
        }
        exit(CJSON::encode($result));
    }

    public function actionDel() {

        $id = Yii::app()->request->getParam("id");
        if ($id > 0) {
            $model = new Menu();
            $idList = explode(',', $id);
            $delete = $model->findByPk($idList);

            if ($delete != null && $delete->delete()) {
                echo CJSON::encode(RMessage::set('true', '', 0));
            } else {
                echo CJSON::encode(RMessage::set('false', $delete->getErrors()));
            }
        } else {
            echo CJSON::encode(RMessage::set('false', Yii::t('default', '参数错误')));
        }
        exit;
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'menu-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}